var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ptbtnblen:true,
    oldbtnblen:false,
    point:0,
    get_w_list:[],
    hidden_bien:true,
    hidden_qiandao:true,
    qiandao_btn: true,
    my_uid: false,
    points_add_invite_points:'',
    spell_list:[],
    spell_list_old:[],
    week: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    // mymucao:{
    //   mc_res:35,
    //   rmb_res:0.35
    // }
  },
  go_front_page(e){
    if (this.data.ptbtnblen){
      wx.navigateTo({
        url: '../pt_front_page/pt_front_page?type=button',
      })
    } else if (this.data.oldbtnblen){
      wx.navigateTo({
        url: '../pt_front_page/pt_front_page?type=old',
      })
    }
  },
  show_tankuang(){
    let that = this;
    that.setData({
      hidden_bien: false
    })
  },
  hide_tankuang() {
//console.log(999);
    let that = this;
    that.setData({
      hidden_bien: true
    })
  },
  qiandao_btn(){
    let that = this;
    this.signin_add();
    that.setData({
      hidden_qiandao: false,
      qiandao_btn: false
    })
    setTimeout(function () {
      that.setData({ hidden_qiandao: true })
    }, 1500)
  },
  select_time(){
    const nowTimeDate = new Date(time)
    return nowTimeDate.setHours(0, 0, 0, 0)
  },
  go_get_money(){
    wx.navigateTo({
      url: '../get_money/get_money',
    })
  },
  go_forage_grass(){
    wx.navigateTo({
      url: '../forage_grass/forage_grass',
    })
  },
  go_invite_courtesy() {
    wx.navigateTo({
      url: '../invite_courtesy/invite_courtesy',
    })
  },
  go_commodity_in_coupon() {
    wx.navigateTo({
      url: '../commodity_in_coupon/commodity_in_coupon',
    })
  },
  go_earn_pasture(){
    wx.navigateTo({
      url: '../earn_pasture/earn_pasture',
    })
  },
  go_stroll_around(){
    wx.navigateTo({
      url: '../stroll_around/stroll_around',
    })
  },
  //获取积分牧草
  add_my_asset(obj) {
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=member_index&op=my_asset',
      data: {
        key: k,
        fields: obj
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
//console.log('获取余额/红包/券/牧草积分:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let available_rc = result.data.datas[obj];
            that.setData({
              [obj]: available_rc
            })
          }
        }
      }
    });
  },
  get_w_list() {//index.php?act=member_signin&op=get_w_list//当前这个星期的签到信息
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=member_signin&op=get_w_list',
      data: {
        key: k
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
        console.log('当前这个星期的签到信息:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let point_res = result.data.datas;
            that.setData({
              get_w_list: point_res
            })
          } else {

          }
        }
      }
    });
  },
  //点击签到
  signin_add() {
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=member_signin&op=signin_add',
      data: {
        key: k
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
console.log('签到:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let point_res = result.data.datas.point;
            that.setData({
              point: point_res
            })
            that.get_w_list();
          }else{

          }
        }
      }
    });
  },
  //检验是否已签到签到
  checksignin() {
    let that = this;
    let k = wx.getStorageSync('key');
    wx.request({
      url: ApiUrl + '/index.php?act=member_signin&op=checksignin',
      data: {
        key: k
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
console.log('检验是否已签到:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let point_res = result.data.datas.point;
            // that.setData({
            //   point: point_res
            // })
          } else {
            that.setData({
              qiandao_btn: false
            })
          }
        }
      }
    });
  },
  //邀请签到得积分-点击邀请链接
  //index.php?act=invite_points&op=points_add
 invite_points_() {
    let that = this;
    let k = wx.getStorageSync('key');
    let fid = wx.getStorageSync('fid');
    wx.request({
      url: ApiUrl + '/index.php?act=invite_points&op=points_add',
      data: {
        key: k,
        uid: fid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (result) {
        console.log('邀请签到得积分-点击邀请链接:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas.point;
            console.log('邀请签到得积分-点击邀请链接:==>', res); 
            that.signin_add(); 
            that.setData({
              points_add_invite_points: res,
              my_uid: false,
              aaaaaa: k,
              bbbbbb: fid
            })
          }
        }
      }
    });
  },
  //拼团-拼团商品列表
  //index.php?act=other&op=spell_list
  spell_list(type){
    let that = this;
    // let k = wx.getStorageSync('key');
    // let uid = wx.getStorageSync('userId');
    // console.log(k, uid, 909090);
    wx.request({
      url: ApiUrl + '/index.php?act=other&op=spell_list',
      data: {
        page:'',
        size:'',
        type: type
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (result) {
        console.log('拼团商品列表:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas.list;
            console.log('拼团商品列表:==>', res);
            that.setData({
              spell_list: res
            })
          }
        }
      }
    });
  },
  spell_list_old() {
    let that = this;
    wx.request({
      url: ApiUrl + '/index.php?act=other&op=spell_list',
      data: {
        page: '',
        size: '',
        type: 'old'
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method:'post',
      success: function (result) {
        console.log('拼团老带新:==>', result);
        if (result.statusCode == 200) {
          if (result.data.code == 200) {
            let res = result.data.datas.list;
            console.log('拼团老带新:==>', res);
            that.setData({
              spell_list_old: res
            })
          }
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  ptbtn(){
    this.spell_list('botton');
    this.setData({
      ptbtnblen: true,
      oldbtnblen:false
    })
  },
  oldbtn(){
    this.spell_list('old');
    this.setData({
      ptbtnblen: false,
      oldbtnblen: true
    })
  },
  onLoad: function (options) {
    let that = this; 
    // let uid = options.uid;
    // wx.showToast({ title: uid, icon: 'none', duration: 10000 });
    if (options.uid) {
      // wx.showToast({ title: uid, icon: 'none', duration: 10000 });
      wx.setStorageSync('fid', options.uid);
      that.setData({
        my_uid: true
      })
      that.invite_points_();
    }
    this.add_my_asset('point');

    this.checksignin();

    this.spell_list('botton');
    // this.spell_list_old();

    this.get_w_list();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.add_my_asset('point');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    let uid = wx.getStorageSync('userId');
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '邀请好友签到，赚牧草',
      imageUrl: '/image/WechatIMG91.jpeg',//图片地址
      path: '/pages/make_money/make_money?uid=' + uid,// 用户点击首先进入的当前页面
      success: function (res) {
        // 转发成功
        console.log("转发成功:");
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:");
      }
    }
  
  }
})